package com.qiandw.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author coxx
 */
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Data
public class SecurityUser extends SysUser implements UserDetails {

    public SecurityUser(SysUser sysUser){
        if(sysUser != null){
            this.setId(sysUser.getId());
            this.setAddress(sysUser.getAddress());
            this.setCreateTime(sysUser.getCreateTime());
            this.setEmail(sysUser.getEmail());
            this.setPassword(sysUser.getPassword());
            this.setPhoneNumber(sysUser.getPhoneNumber());
            this.setRealName(sysUser.getRealName());
            this.setSysRoles(sysUser.getSysRoles());
            this.setUsername(sysUser.getUsername());
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.getSysRoles().stream().map(x -> new SimpleGrantedAuthority(x.getName())).collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
